package logic

import (
	"context"
	"develop/model/cache"
	"develop/model/mysql"
	"fmt"
	"k8s.io/apimachinery/pkg/util/json"
	"time"

	"develop/usersrpc/internal/svc"
	"develop/usersrpc/usersrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type TopicListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTopicListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TopicListLogic {
	return &TopicListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TopicListLogic) TopicList(in *usersrpc.TopicListRequest) (*usersrpc.TopicListResponse, error) {
	// todo: add your logic here and delete this line
	key := fmt.Sprintf("topicList%d", in.VideoId)
	var topicList []*usersrpc.TopicList
	result, _ := cache.Rdb.Get(l.ctx, key).Result()
	if result == "" {
		topicList = mysql.TopicList(mysql.DB, in)
		marshal, _ := json.Marshal(topicList)
		cache.Rdb.Set(l.ctx, key, marshal, time.Minute*10)
	} else {
		json.Unmarshal([]byte(result), &topicList)
	}

	return &usersrpc.TopicListResponse{
		List: topicList,
	}, nil
}
